package cn.kent.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;


/**
 * 演示队列设置过期时间
 */
@Configuration
public class RabbitQueueConfig {
    public static final String QUEUE_DELAY_QUEUE_NAME = "queue_delay_queue_name";
    public static final String QUEUE_DELAY_EXCHANGE_NAME = "queue_delay_exchange_name";

    @Bean
    Queue queueDelayQueue() {
        // 该队列的消息如果10s之内无人消费，则消息过期
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-message-ttl", 10000);
        return new Queue(QUEUE_DELAY_QUEUE_NAME, true, false, false, args);
    }

    @Bean
    DirectExchange directExchange2() {
        return new DirectExchange(QUEUE_DELAY_EXCHANGE_NAME, true, false);
    }

    @Bean
    Binding queueDelayQueueBinding() {
        return BindingBuilder.bind(queueDelayQueue())
                .to(directExchange2())
                .with(QUEUE_DELAY_QUEUE_NAME);
    }
}
